#include "stdafx.h"
#include "GButton.h"
#include "GResLoad.h"

GButton::GButton(void)
{
	m_btnState = MOUSEOVER;
}

GButton::~GButton(void)
{
}
void GButton::LoadResAttritute(map<tstring, tstring>& mapAtt)
{
	map<tstring, tstring>::iterator iter;
	iter = mapAtt.find(_T("mouseover"));
	if (iter == mapAtt.end()) assert(0);
	m_btntexture[MOUSEOVER] = GImageLoad::LoadTextureFromFile(iter->second.c_str());

	iter = mapAtt.find(_T("mousemove"));
	if (iter == mapAtt.end()) assert(0);
	m_btntexture[MOUSEMOVE] = GImageLoad::LoadTextureFromFile(iter->second.c_str());

	iter = mapAtt.find(_T("mousedown"));
	if (iter == mapAtt.end()) assert(0);
	m_btntexture[MOUSEDOWN] = GImageLoad::LoadTextureFromFile(iter->second.c_str());

	iter = mapAtt.find(_T("name"));
	if (iter == mapAtt.end()) assert(0);
	m_strContext = iter->second;

	GWnd::LoadResAttritute(mapAtt);
}
void GButton::Draw()
{
	GRECT rc(m_nLeft, m_nBotton, m_nRight, m_nTop);
	rc.Offset(m_nParentLeft, m_nParentBotton);

	glBindTexture(GL_TEXTURE_2D, m_btntexture[m_btnState]);
	glBegin(GL_QUADS);
	glTexCoord2f( 0, 0);glVertex2f(rc.left, rc.botton);  
	glTexCoord2f( 1, 0);glVertex2f(rc.right, rc.botton);
	glTexCoord2f( 1, 1);glVertex2f(rc.right, rc.top);
	glTexCoord2f( 0, 1);glVertex2f(rc.left, rc.top);	
	glEnd();
	rc.top +=8;
	m_font.TextLine(m_strContext.c_str(), GRGB(255,0,0), rc);
}
void GButton::MouseMove(int x, int y,int wParam)
{
	if (IsPointInWnd(x, y))
	{
		m_btnState = MOUSEMOVE;
	}
	else
	{
		m_btnState = MOUSEOVER;
	}
}

void GButton::MouseClick(int button, int state, int x, int y)
{
	if (IsPointInWnd(x, y))
	{		
 		if (state)
 		{
 			m_btnState = MOUSEMOVE;
 		}
 		else
 		{
 			m_btnState = MOUSEDOWN;
		}
		
		if (m_btnState == MOUSEDOWN)
		{
			m_pParentWnd->HandleEvent(m_hWnd);
		}	
	}


}